package com.kwt.learn.map;

import java.math.BigDecimal;
import java.util.HashMap;

import static java.math.BigDecimal.ROUND_HALF_UP;

public class HashMapLearn {
    public static void main(String[] args) {
        HashMap<String, String> map = new HashMap<String, String>(23);
//        map.put("key", "你好");

//        System.err.println("Aa".hashCode());
//        System.err.println("BB".hashCode());
//        //插入两个hashcode相同的key
//        map.put("Aa", "a");
//        map.put("BB", "b");
//
//        System.err.println(map);

// 制造一些字母找到hashcode相同的一些字符串
        int len = 3;
        char[] chars = new char[len];
        tryBit(chars, len);
        System.err.println((int) Math.pow(offset, len) + ":" + dup);

    }


    static HashMap map = new HashMap();
    private static char startChar = 'A';
    private static char endChar = 'z';
    private static int offset = endChar - startChar + 1;
    private static int dup = 0;

    private static void tryBit(char[] chars, int i) {
        for (char j = startChar; j <= endChar; j++) {
            chars[i - 1] = j;
            if (i > 1)
                tryBit(chars, i - 1);
            else
                test(chars);
        }
    }

    private static void test(char[] chars) {
//        String str = new String(chars).replaceAll("[^a-zA-Z_]", "").toUpperCase();// 195112:0
//        String str = new String(chars).toLowerCase();//195112:6612
        String str = new String(chars).replaceAll("[^a-zA-Z_]", "");//195112:122500
//        String str = new String(chars);//195112:138510
        int hash = str.hashCode();
        if (map.containsKey(hash)) {
            String s = (String) map.get(hash);
            if (!s.equals(str)) {
                dup++;
                System.err.println(s + ":" + str + ",hashcode:" + hash + ",equals:" + s.equals(str));
            }
        } else {
            map.put(hash, str);
//            System.out.println(str);
        }
    }


    /**
     * hashmap 泊松分布
     *
     * https://blog.csdn.net/weixin_43883685/article/details/109809049
     */
}
